import api from './index'

export const cmdbAPI = {
  // 环境管理
  getEnvironments() {
    return api.get('/cmdb/environments/')
  },

  createEnvironment(data) {
    return api.post('/cmdb/environments/', data)
  },

  updateEnvironment(id, data) {
    return api.put(`/cmdb/environments/${id}/`, data)
  },

  deleteEnvironment(id) {
    return api.delete(`/cmdb/environments/${id}/`)
  },

  // 主机类型管理
  getHostTypes() {
    return api.get('/cmdb/host-types/')
  },

  createHostType(data) {
    return api.post('/cmdb/host-types/', data)
  },

  updateHostType(id, data) {
    return api.put(`/cmdb/host-types/${id}/`, data)
  },

  deleteHostType(id) {
    return api.delete(`/cmdb/host-types/${id}/`)
  },

  // 主机组管理
  getHostGroups() {
    return api.get('/cmdb/host-groups/')
  },

  createHostGroup(data) {
    return api.post('/cmdb/host-groups/', data)
  },

  updateHostGroup(id, data) {
    return api.put(`/cmdb/host-groups/${id}/`, data)
  },

  deleteHostGroup(id) {
    return api.delete(`/cmdb/host-groups/${id}/`)
  },

  getHostGroupHosts(id) {
    return api.get(`/cmdb/host-groups/${id}/hosts/`)
  },

  // 主机管理
  getHosts(params) {
    return api.get('/cmdb/hosts/', { params })
  },

  getHost(id) {
    return api.get(`/cmdb/hosts/${id}/`)
  },

  createHost(data) {
    return api.post('/cmdb/hosts/', data)
  },

  updateHost(id, data) {
    return api.put(`/cmdb/hosts/${id}/`, data)
  },

  deleteHost(id) {
    return api.delete(`/cmdb/hosts/${id}/`)
  },

  // 批量删除主机
  batchDeleteHosts(hostIds) {
    return api.post('/cmdb/hosts/batch_delete/', { host_ids: hostIds })
  },

  testConnection(id) {
    return api.post(`/cmdb/hosts/${id}/test_connection/`)
  },

  executeCommand(id, command) {
    return api.post(`/cmdb/hosts/${id}/execute_command/`, { command })
  },

  // 收集主机信息
  collectHostInfo(id) {
    return api.post(`/cmdb/hosts/${id}/collect_info/`)
  },

  // 批量收集主机信息
  batchCollectHostInfo() {
    return api.post('/cmdb/hosts/batch_collect_info/')
  },



  // 获取 Celery 监控服务状态
  getMonitoringStatus() {
    return api.get('/cmdb/hosts/monitoring_status/')
  },

  // 批量导入主机
  batchImportHosts(hostsData) {
    return api.post('/cmdb/hosts/batch_import/', hostsData)
  },

  getHostMonitorData(id) {
    return api.get(`/cmdb/hosts/${id}/monitor_data/`)
  },

  batchTestConnection(hostIds) {
    return api.post('/cmdb/hosts/batch_test_connection/', { host_ids: hostIds })
  },

  // 主机监控数据
  getMonitorData(params) {
    return api.get('/cmdb/host-monitor-data/', { params })
  },

  // 数据库实例管理
  getDatabaseInstances() {
    return api.get('/cmdb/database-instances/')
  },

  createDatabaseInstance(data) {
    return api.post('/cmdb/database-instances/', data)
  },

  updateDatabaseInstance(id, data) {
    return api.put(`/cmdb/database-instances/${id}/`, data)
  },

  deleteDatabaseInstance(id) {
    return api.delete(`/cmdb/database-instances/${id}/`)
  },

  testDatabaseConnection(id) {
    return api.post(`/cmdb/database-instances/${id}/test_connection/`)
  },

  // SQL审计日志
  getSQLAuditLogs(params) {
    return api.get('/cmdb/sql-audit-logs/', { params })
  },

  createSQLAuditLog(data) {
    return api.post('/cmdb/sql-audit-logs/', data)
  },

  approveSQLAuditLog(id, comment) {
    return api.post(`/cmdb/sql-audit-logs/${id}/approve/`, { comment })
  },

  rejectSQLAuditLog(id, comment) {
    return api.post(`/cmdb/sql-audit-logs/${id}/reject/`, { comment })
  },

  executeSQLAuditLog(id) {
    return api.post(`/cmdb/sql-audit-logs/${id}/execute/`)
  },

  // 仪表盘统计数据
  getDashboardStats() {
    return api.get('/cmdb/dashboard/stats/')
  }
}
